/**
 * Kugou 手机内嵌页通信接口v2.2
 * 异步回调方式
 *
 * @Module KgMobileCall
 *
*/
/* eslint-disable */
'use strict';
var external = external || window.external;
let ua = navigator.userAgent;
let ipad = ua.match(/(iPad).*OS\s([\d_]+)/),
ipod = ua.match(/(iPod)(.*OS\s([\d_]+))?/),
iphone = !ipad && ua.match(/(iPhone\sOS)\s([\d_]+)/);
export const KgMobileCall = {
	/*库版本*/
	ver : '2.2',
	/**
	* 是否调试模式
	* 0 : 关闭
	* 1 ：console
	* 2 : alert
	*/
	debug : 0,
	/*是否在安卓内嵌页*/
	isAndroid : (typeof external == 'undefined') ? false : ((typeof external.superCall == 'undefined') ? false : true),
	/*是否在IOS内嵌页*/
	isIOS : ua.match(/KGBrowser/gi) ? true : false,
	chkAndroidApp : function(){
		try{
			if(external.superCall(122)){
				return true;
			}
		}catch(ex){
			return false;
		}
    },
    /*判断是否是android系统 */
    isAndroidSys: /android/gi.test(ua),
    /*判断是否为ios系统 */
    iosSys: ipad || iphone || ipod,
    /*判断是否在酷狗唱唱*/
    inSingleApp: ua.match(/KTVBrowser/gi) ? true : false,
	cbNum : 0,
	/*链接格式*/
	formatURL : {
		"browser":"",
		"url":""
	},
	/*歌曲格式数据*/
	formatSong : {
		"filename": "",
		"filesize": "",
		"hash": "",
		"bitrate": "",
		"extname": "",
		"duration": "",
		"mvhash": "",
		"m4afilesize": "",
		"320hash": "",
		"320filesize": "",
		"sqhash": "",
		"sqfilesize": 0,
		"feetype": 0,
		"isfirst": 0
	},
	/*MV格式数据*/
	formatMV : {
		"filename": "",
		"singername": "",
		"hash": "",
		"imgurl": ""
	},
	/*分享数据*/
	formatShare : {
		"shareName" : "",
		"topicName" : "",
		"hash" : "",
		"listID" : "",
		"type" : "",
		"suid" : "",
		"slid" : "",
		"imgUrl" : "",
		"filename" : "",
		"duration" :"",
		"shareData" : {
			"linkUrl" : "",
			"picUrl" : "",
			"content" : "",
			"title" : ""
		}
	},
	/**
	 * 格式化数据,覆盖,1层递归
	* @param { Object } osrc，格式数据
	* @param { Object } src， 数据
	*/
	_extend : function(osrc, src){
		var me = this;

		if(src){
			for (var property in osrc) {
				if(!src.hasOwnProperty(property) && src.propertyIsEnumerable(property)){
					src[property] = osrc[property];
				}
			}
		}
		return src;
	},
	/**
	* @method str2Json
	*
	* @param { String } jsonStr
	* @return { Object } json
	*/
	str2Json : function(jsonStr){
		var json = {};
		if(Object.prototype.toString.call(jsonStr) === "[object String]"){
			try {
				json = JSON.parse(jsonStr);
			} catch (ex) {
				json = {};
			}
		}
		return json;
	},
	json2Str : function(json){
		var jsonStr = "";
		if(typeof json !== "string"){
			try {
				jsonStr = JSON.stringify(json);
			} catch (e) {
				jsonStr = "";
			}
		}
		return jsonStr;
	},
	/**
     * 执行不返回数据的调用
     *
     * @method callCmd
     * @function
     * @param { Object } {cmd:Number(命令号),jsonStr:String(参数),callback:Function(回调函数)}
     * @return void(0)
     */
	callCmd: function(o) {
		var me = this,
		iosApi = "",
		res,
		dc = document;

		//debug
		if(dc.getElementById('mod_debugwin_set')){
			dc.getElementById('mod_debugwin_set').innerHTML = JSON.stringify(o).replace(/\\/g,'');
		}
		if(me.debug > 0){
			switch(me.debug) {
				case 1 :
					if(window.console){
						console.log(o);
					}
					break;
				case 2 :
					alert(JSON.stringify(o).replace(/\\/g,''));
					break;
				default:
					break;
			}
		}

		if(me.chkAndroidApp() || (me.inSingleApp && me.isAndroidSys)){
			try{
				if(o.jsonStr){
					res = external.superCall(o.cmd, o.jsonStr);
				}else{
					res = external.superCall(o.cmd);
				}
				if(o.callback && o.callback !== '' && res){
					res = me.str2Json(res);
					o.callback(res);
				}
			}catch(ex){}
		} else if(me.isIOS || (me.inSingleApp && me.iosSys)){
			if(o.callback){
				me.cbNum++;
				var cbName = "kgmobilecall" + me.cbNum + Math.random().toString().substr(2,9);
				/*创建随机函数*/
				window[cbName] = function(res){
					if(typeof res != 'undefined'){
						if(o.callback){
							if(Object.prototype.toString.call(res) === '[object String]'){
								o.callback(me.str2Json(res));
							}else{
								o.callback(res);
							}
						}
					}
					//delete window[cbName];
				};

				if(o.jsonStr){
					iosApi = 'kugouurl://start.music/?{"cmd":'+ o.cmd +', "jsonStr":'+ o.jsonStr +', "callback":"'+ cbName +'"}';
				}else{
					iosApi = 'kugouurl://start.music/?{"cmd":'+ o.cmd +', "callback":"'+ cbName +'"}';
				}
			}else{
				if(o.jsonStr){
					iosApi = 'kugouurl://start.music/?{"cmd":'+ o.cmd +', "jsonStr":'+ o.jsonStr +'}';
				}else{
					iosApi = 'kugouurl://start.music/?{"cmd":'+ o.cmd +'}';
				}
            }
            if(me.inSingleApp){
				iosApi = iosApi.replace('kugouurl', "kugouKGE");
			}
			location.href = iosApi;
		}else {
			if(o.callback){
				o.callback({"status": 0, "error":"不在客户端内"});
			}
		}
	},
	/**
     * 获取登录用户信息
     *
     * @method getUserInfo
     * @function
	 * @param { String } 回调函数名
     */
	getUserInfo: function(callback) {
		var me = this;

		if(callback){
			me.callCmd({cmd:101, callback:callback});
		} else {
			me.callCmd({cmd:101});
		}
	},
	/**
     * 弹出登录窗口
     *
     * @method callSoftUserLogin
	 *
	 * @param { Object } json参数
	 */
	callSoftUserLogin: function(json, callback) {
		var me = this,
		jsonStr = "";

		if(json){

			if(!json.loginType){
				json.loginType = '';
			}

			jsonStr = me.json2Str(json);
			if(callback){
				me.callCmd({cmd: 102, jsonStr: jsonStr, callback: callback});
			}else{
				me.callCmd({cmd: 102, jsonStr: jsonStr});
			}
		}else{
			if(callback){
				me.callCmd({cmd: 102, callback: callback});
			}else{
				me.callCmd({cmd: 102});
			}
		}
	},
	/**
     * 弹出注册窗口
     *
     * @method showRegisterBox
     * @function
     */
	showRegisterBox: function(json, callback) {
		var me = this,
		jsonStr = "";

		if(json){
			jsonStr = me.json2Str(json);
			if(callback){
				me.callCmd({cmd: 103, jsonStr: jsonStr, callback: callback});
			}else{
				me.callCmd({cmd: 103, jsonStr: jsonStr});
			}
		}else{
			if(callback){
				me.callCmd({cmd: 103, callback: callback});
			}else{
				me.callCmd({cmd: 103});
			}
		}
	},
	/**
     * 退出登录
     *
     * @method logout
     */
	logout: function(callback) {
		var me = this;
		if(callback){
			me.callCmd({cmd:104, callback: callback});
		}else{
			me.callCmd({cmd:104});
		}
	},
    /**
     * 下载歌曲不播放
     *
     * @method downNoPlay
	 * @param { Object } JSON参数
	 */
    downNoPlay : function(json, callback){
		var me = this,
		data = [],
		info = [],
		jsonStr = "";

		if(json){
			data = json.info;

			if(Object.prototype.toString.call(data) === '[object Array]'){
				for (var i = 0, len = data.length; i < len; i++) {
					info.push(me._extend(me.formatSong, data[i]));
				}
			}

			//保存回json
			json.info = info;

			if(!json.total){
				json.total = json.info.length;
			}
			if(!json.className){
				json.className = '';
			}

			//转成字符串JSON
			jsonStr = me.json2Str(json);

			if(callback){
				me.callCmd({cmd:107, jsonStr:jsonStr, callback: callback});
			}else{
				me.callCmd({cmd:107, jsonStr:jsonStr});
			}
		}
    },
    /**
     * 歌曲试听不下载(添加到默认列表)
     *
     * @method listen
	 * @param { Object } JSON参数
	 */
    listen : function(json, callback){
		var me = this,
		data = [],
		info = [],
		jsonStr = "";

		if(json){
			data = json.info;

			if(Object.prototype.toString.call(data) === '[object Array]'){
				for (var i = 0, len = data.length; i < len; i++) {
					info.push(me._extend(me.formatSong, data[i]));
				}
			}

			//保存回json
			json.info = info;

			/*歌曲数*/
			if(!json.total){
				json.total = json.info.length;
			}
			/*列表名*/
			if(!json.className){
				json.className = '';
			}
			/*播放序列*/
			if(!json.playIndex){
				json.playIndex = 0;
			}
			/*打开播放页*/
			if(!json.isJump){
				json.isJump = 0;
			}
			/*音效*/
			if(!json.effect){
				json.effect = 0;
			}

			//转成字符串JSON
			jsonStr = me.json2Str(json);

			if(callback){
				me.callCmd({cmd:108, jsonStr:jsonStr, callback: callback});
			}else{
				me.callCmd({cmd:108, jsonStr:jsonStr});
			}
		}
    },
    /**
     * 创建新列表
     *
     * @method createList
     * @function
     * @param { String } JSON string
     * @return
     */
    createList : function(json, callback){
		var me = this,
		data = [],
		info = [],
		jsonStr = "";

		if(json){
			data = json.info;

			if(Object.prototype.toString.call(data) === '[object Array]'){
				for (var i = 0, len = data.length; i < len; i++) {
					info.push(me._extend(me.formatSong, data[i]));
				}
			}

			//保存回json
			json.info = info;

			if(!json.total){
				json.total = json.info.length;
			}
			if(!json.className){
				json.className = '';
			}

			//转成字符串JSON
			jsonStr = me.json2Str(json);

			if(callback){
				me.callCmd({cmd:109, jsonStr:jsonStr, callback: callback});
			}else{
				me.callCmd({cmd:109, jsonStr:jsonStr});
			}
		}
    },
    /**
     * 歌曲添加不下载
     *
     * @method addNoDown
	 * @param { Object } JSON参数
	 */
    addNoDown : function(json, callback){
		var me = this,
		data = [],
		info = [],
		jsonStr = "";

		if(json){
			data = json.info;

			if(Object.prototype.toString.call(data) === '[object Array]'){
				for (var i = 0, len = data.length; i < len; i++) {
					info.push(me._extend(me.formatSong, data[i]));
				}
			}

			//保存回json
			json.info = info;

			if(!json.total){
				json.total = json.info.length;
			}
			if(!json.className){
				json.className = '';
			}

			//转成字符串JSON
			jsonStr = me.json2Str(json);

			if(callback){
				me.callCmd({cmd:110, jsonStr:jsonStr, callback: callback});
			}else{
				me.callCmd({cmd:110, jsonStr:jsonStr});
			}
		}
    },
    /**
     * 添加到当前歌曲下一位置稍后播放（插播）
     *
     * @method spots
	 * @param { Object } JSON
	 */
    spots : function(json, callback){
		var me = this,
		data = [],
		info = [],
		jsonStr = "";

		if(json){
			data = json.info;

			if(Object.prototype.toString.call(data) === '[object Array]'){
				for (var i = 0, len = data.length; i < len; i++) {
					info.push(me._extend(me.formatSong, data[i]));
				}
			}

			//保存回json
			json.info = info;

			if(!json.total){
				json.total = json.info.length;
			}
			if(!json.className){
				json.className = '';
			}

			//转成字符串JSON
			jsonStr = me.json2Str(json);

			if(callback){
				me.callCmd({cmd:111, jsonStr:jsonStr, callback: callback});
			}else{
				me.callCmd({cmd:111, jsonStr:jsonStr});
			}
		}
    },
    /**
     * 创建歌单列表并播放歌曲并下载
     *
     * @method createListDownAndPlaySong
     * @param { Object } JSON参数
     */
    createListDownAndPlaySong : function(json, callback){
		var me = this,
		data = [],
		info = [],
		jsonStr = "";

		if(json){
			data = json.info;

			if(Object.prototype.toString.call(data) === '[object Array]'){
				for (var i = 0, len = data.length; i < len; i++) {
					info.push(me._extend(me.formatSong, data[i]));
				}
			}

			//保存回json
			json.info = info;

			if(!json.total){
				json.total = json.info.length;
			}
			if(!json.className){
				json.className = '';
			}

			//转成字符串JSON
			jsonStr = me.json2Str(json);

			if(callback){
				me.callCmd({cmd:112, jsonStr:jsonStr, callback: callback});
			}else{
				me.callCmd({cmd:112, jsonStr:jsonStr});
			}
		}
    },
    /**
     * 创建新列表并下载歌曲不播放
     *
     * @method createListDownNoPlaySong
	 * @param { Object } JSON参数
	 */
    createListDownNoPlaySong : function(json, callback){
		var me = this,
		data = [],
		info = [],
		jsonStr = "";

		if(json){
			data = json.info;

			if(Object.prototype.toString.call(data) === '[object Array]'){
				for (var i = 0, len = data.length; i < len; i++) {
					info.push(me._extend(me.formatSong, data[i]));
				}
			}

			//保存回json
			json.info = info;

			//转成字符串JSON
			jsonStr = me.json2Str(json);

			if(callback){
				me.callCmd({cmd:113, jsonStr:jsonStr});
			}else{
				me.callCmd({cmd:113, jsonStr:jsonStr, callback: callback});
			}
		}
    },
    /**
     * 创建专辑收藏并添加歌曲
     *
     * @method createCollectListAddSong
	 * @param { Object } JSON参数
	 */
    createCollectListAddSong : function(json, callback){
		var me = this,
		data = [],
		info = [],
		jsonStr = "";

		if(json){
			data = json.info;

			if(Object.prototype.toString.call(data) === '[object Array]'){
				for (var i = 0, len = data.length; i < len; i++) {
					info.push(me._extend(me.formatSong, data[i]));
				}
			}

			//保存回json
			json.info = info;

			if(!json.total){
				json.total = json.info.length;
			}
			if(!json.className){
				json.className = '';
			}

			//转成字符串JSON
			jsonStr = me.json2Str(json);
			if(callback){
				me.callCmd({cmd:114, jsonStr:jsonStr, callback: callback});
			}else{
				me.callCmd({cmd:114, jsonStr:jsonStr});
			}
		}
    },
    /**
     * 分享歌曲/专辑/歌单
     *
     * @method share
     * @function
     * @param { Object } json 参数
     * @param { Function } [iosCallback]
	 * @param { Function } [weixinCallback] 如果此参数为空时，在非客户端非微信的情况下就会调用新浪分享，不为空时就会调用此callback。。。
	 * @param { Function } [otherCallback] 其他回调
     * @return
     */
    share : function(json, iosCallback, weixinCallback, otherCallback){
		var me = this,
		jsonStr = "",
		info = {},
		ua = navigator.userAgent,
		_wbShare = function(){
			var option = {
				title: encodeURIComponent(json.shareData.title) || "" ,
				url: json.shareData.linkUrl || "",
				content: encodeURIComponent(json.shareData.content) || "",
				imgSrc: encodeURI(json.shareData.picUrl) || "",
				swf:"",
				appkey:"340086183",
				/*(新浪微博) 用参数,关联用户的UID，分享微博会@该用户(可选)*/
				ralateUid:""
			},
			winSrc = "",
			queryStr = "";

			winSrc = "http://service.t.sina.com.cn/share/share.php";
			queryStr = [
				"url=" + option.url,
				"appkey=" + option.appkey,
				"title=" + option.content,
				"pic=" + option.imgSrc,
				"ralateUid=" + option.ralateUid
			].join("&");

			location.href = winSrc + "?" + queryStr;
		};

		if(json){
			info = me._extend(me.formatShare, json);
			json = info;
		}

		//TODO
		if(me.isIOS){
			if(iosCallback){
				me.callCmd({cmd : 610, jsonStr : me.json2Str(json), callback : iosCallback});
			}else{
				me.callCmd({cmd : 610, jsonStr : me.json2Str(json)});
			}
		}else if(me.chkAndroidApp()){
			me.getVersion(function(res){
				if(res && json && json.type == 3 && res.platform == "android" && Number(res.version) < 6340){
					_wbShare();
				}else{
					me.callCmd({cmd : 610, jsonStr : me.json2Str(json)});
				}
			});
		}else if(ua.match('MicroMessenger')){
			if(weixinCallback){
				weixinCallback();
			}else{
				_wbShare();
			}
		}else{
			if(otherCallback){
				otherCallback();
			}else{
				_wbShare();
			}
		}
    },
    /**
     * 调用客户端搜索
     *
     * @method openSearch
     * @function
     * @param jsonStr
     * @return
     */
    openSearch : function(json, callback){
		var me = this,
		jsonStr = "";
        if(json){
			jsonStr = me.json2Str(json);
			if(callback){
				me.callCmd({cmd:116,jsonStr:jsonStr, callback: callback});
			}else{
				me.callCmd({cmd:116,jsonStr:jsonStr});
			}
        }else{
            me.callCmd({cmd:116});
        }
    },
    /**
     * 播放MV
     *
     * @method playMV
     * @function
     * @param json
     * @return
     */
    playMV : function(json, callback){
		var me = this,
		data = [],
		info = [],
		jsonStr = "";

		if(json){
			data = json.info;

			if(Object.prototype.toString.call(data) === '[object Array]'){
				for (var i = 0, len = data.length; i < len; i++) {
					info.push(me._extend(me.formatMV, data[i]));
				}
			}

			jsonStr = me.json2Str(json);
			if(callback){
				me.callCmd({cmd:117,jsonStr:jsonStr, callback: callback});
			}else{
				me.callCmd({cmd:117,jsonStr:jsonStr});
			}
		}
    },
    /**
     * 播放电台
     *
     * @method playRadio
     * @function
     * @return
     */
    playRadio : function(json, callback){
		var me = this,
		jsonStr = "";

		if(json){
			jsonStr = me.json2Str(json);
			if(callback){
				me.callCmd({cmd:119,jsonStr:jsonStr, callback: callback});
			}else{
				me.callCmd({cmd:119,jsonStr:jsonStr});
			}
		}
    },
    /**
     * 获取当前正在播放的电台信息
     *
     * @method getRadioStatus
     * @function
     * @return
     */
    getRadioStatus : function(callback){
		var me = this;
		if(callback){
			me.callCmd({cmd:121, callback: callback});
		}else{
			me.callCmd({cmd:121});
		}
    },
    /**
     * 获取当前酷狗客户端版本号
     *
     * @method getVersion
	 * @param { String } callback
	 */
    getVersion: function(callback) {
		this.callCmd({cmd:122, callback:callback});
    },
    /**
     * 调用系统浏览器打开链接
     *
     * @method openURL
     * @function
     * @return
     */
    openURL : function(json, callback){
		var me = this,
		jsonStr = "";

		if(json){
			json = me._extend(me.formatURL, json);

			jsonStr = me.json2Str(json);
			if(callback){
				me.callCmd({cmd:123, jsonStr:jsonStr, callback: callback});
			}else{
				me.callCmd({cmd:123, jsonStr:jsonStr});
			}
		}else{
			me.callCmd({cmd:123});
		}
    },
    /**
     * 获取当前手机信息
     *
     * @method getMobileInfo
     * @function
     * @return
     */
    getMobileInfo : function(callback){
        this.callCmd({cmd:124, callback:callback});
    },
    /**
     * 通知客户端暂停/续播歌曲
     *
     * @method playOrPause
     * @function
     * @return
     */
    playOrPause : function(json, callback){
		var me = this,
		jsonStr = "";

		if(!json){
			json = {
				type : 1
			};
		}

		if(json){
			jsonStr = me.json2Str(json);
			if(callback){
				me.callCmd({cmd:125, jsonStr:jsonStr, callback: callback});
			}else{
				me.callCmd({cmd:125, jsonStr:jsonStr});
			}
		}
    },
    /**
     * 打开客户端相关页面
     * tab : "main" //main : 主页,musicLib : 乐库主页,singer : 歌手页,local : 附近,more : 更多,app : 应用推荐
     *
     * @method openTab
     * @function
     * @param json
     * @return
     */
    openTab : function(json, callback){
		var me = this,
		jsonStr = "";
		if(json){
			jsonStr = me.json2Str(json);
			if(callback){
				me.callCmd({cmd:126, jsonStr:jsonStr, callback: callback});
			}else{
				me.callCmd({cmd:126, jsonStr:jsonStr});
			}
		}else{
			if(callback){
				me.callCmd({cmd:126, callback: callback});
			}else{
				me.callCmd({cmd:126});
			}
		}
    },
    clearListAndAddSongs : function(json, callback){
		var me = this,
		data = [],
		info = [],
		jsonStr = "";

		if(json){
			data = json.info;

			if(Object.prototype.toString.call(data) === '[object Array]'){
				for (var i = 0, len = data.length; i < len; i++) {
					info.push(me._extend(me.formatSong, data[i]));
				}
			}

			//保存回json
			json.info = info;

			//转成字符串JSON
			jsonStr = me.json2Str(json);

			if(callback){
				me.callCmd({cmd:127, jsonStr:jsonStr, callback: callback});
			}else{
				me.callCmd({cmd:127, jsonStr:jsonStr});
			}
        }else{
			if(callback){
				me.callCmd({cmd:127, callback: callback});
			}else{
				me.callCmd({cmd:127});
			}
        }
    },
	/**
	 * 显示隐藏Bar条
	 */
	showHideBar : function(json, callback){
		var me = this,
		jsonStr = '';

		if(!json){
			json = {
				state : 1
			};
		}

		jsonStr = me.json2Str(json);

		if(callback){
			me.callCmd({cmd:128, jsonStr:jsonStr, callback: callback});
		}else{
			me.callCmd({cmd:128, jsonStr:jsonStr});
		}
	},
	/**
	 * 重力感应（设备移动的加速度）
	 *
	 */
	getAcceleration : function(callback){
		var me = this;

		if(callback){
			me.callCmd({cmd:129, callback: callback});
		}
	},
	/**
	 * 陀螺仪
	 *
	 */
	getOrientation : function(callback){
		var me = this;

		if(callback){
			me.callCmd({cmd:130, callback: callback});
		}
	},
    /**
     * 是否在客户端里面
     *
     * @method isClient
     * @function
     * @return
     */
    isInClient : function(){
		var me = this;
		if(me.chkAndroidApp()){
			return me.chkAndroidApp();
		}else if(me.isIOS){
			return me.isIOS;
		}else{
			return false;
		}
    },
	/**
	 * 打开摄像头
	 */
	openCamera : function(json, callback){
		var me = this,
		jsonStr = '';

		if(!json){
			json = {
				type : 1
			};
		}

		if(json.size){
			json.size = {
				maxWidth : '',
				minWidth : '',
				maxHeight:'',
				minHeight: ''
			};
		}

		jsonStr = me.json2Str(json);

		if(callback){
			me.callCmd({cmd:131, jsonStr:jsonStr, callback: callback});
		}else{
			me.callCmd({cmd:131, jsonStr:jsonStr});
		}
	},
	/**
	 * 充值vip类型
	 */
	recharge : function(json, callback){
		var me = this,
		jsonStr = '';

		if(!json){
			return;
		}

		jsonStr = me.json2Str(json);

		if(callback){
			me.callCmd({cmd:132, jsonStr:jsonStr, callback: callback});
		}else{
			me.callCmd({cmd:132, jsonStr:jsonStr});
		}
	},
	/**
	 * 关闭/打开摇一摇
	 */
	switchShark : function(json, callback){
		var me = this,
		jsonStr = '';

		if(!json){
			return;
		}

		if(isNaN(parseInt(json.type, 10))){
			json.type = 0;
		}

		jsonStr = me.json2Str(json);

		if(callback){
			me.callCmd({cmd:133, jsonStr:jsonStr, callback: callback});
		}else{
			me.callCmd({cmd:133, jsonStr:jsonStr});
		}
	},
	/**
	 * 下载特权的回调
	 *
	 */
	downloadPrivileges : function(callback){
		var me = this;

		if(callback){
			me.callCmd({cmd:135, callback: callback});
		}
	},
	/**
	 * 获取繁星用户信息
	 */
	getFxUserInfo : function(callback){
		var me = this;

		if(callback){
			me.callCmd({cmd:400, callback: callback});
		}else{
			me.callCmd({cmd:400});
		}
	},
	/**
	 * 获取KTV用户信息
	 */
	getKTVUserInfo : function(callback){
		var me = this;

		if(callback){
			me.callCmd({cmd:500, callback: callback});
		}else{
			me.callCmd({cmd:500});
		}
	},
	/**
	 * 跳转看唱模块各个内页
	 */
	jumpToKFInnerPage : function(json, callback){
		var me = this,
		jsonStr = '';

		if(!json){
			return;
		}

		if(!json.subPageId){
			json.subPageId = 0;
		}

		if(json.params === ''){
			delete json.params;
		}

		jsonStr = me.json2Str(json);

		if(callback){
			me.callCmd({cmd:600, jsonStr:jsonStr, callback: callback});
		}else{
			me.callCmd({cmd:600, jsonStr:jsonStr});
		}
	},
	/**
	 * 展示js的消息内容
	 */
	showMsg : function(json, callback){
		var me = this,
		jsonStr = '';

		if(!json){
			return;
		}

		if(!json.msg){
			json.msg = '';
		}

		if(!json.type){
			json.type = 1;
		}

		jsonStr = me.json2Str(json);

		if(callback){
			me.callCmd({cmd:601, jsonStr:jsonStr, callback: callback});
		}else{
			me.callCmd({cmd:601, jsonStr:jsonStr});
		}
	},
	/**
	 * 更新客户端内嵌页标题
	 */
	updateTitle : function(json, callback){
		var me = this,
		jsonStr = '';

		if(!json){
			return;
		}

		if(!json.title){
			json.title = '';
		}

		jsonStr = me.json2Str(json);

		if(callback){
			me.callCmd({cmd:602, jsonStr:jsonStr, callback: callback});
		}else{
			me.callCmd({cmd:602, jsonStr:jsonStr});
		}
	},
	/**
	 * 充值音乐包会员
	 */
	rechargeMusic : function(json, callback){
		var me = this,
		jsonStr = '';

		if(!json){
			return;
		}

		jsonStr = me.json2Str(json);

		if(callback){
			me.callCmd({cmd:134, jsonStr:jsonStr, callback: callback});
		}else{
			me.callCmd({cmd:134, jsonStr:jsonStr});
		}
	},
	/**
	 * 下载特权的回调接口
	 *
	 */
	/*downloadPrivileges : function(callback){
		var me = this;

		if(callback){
			me.callCmd({cmd:135, callback: callback});
		}
	},*/
	/**
	 * VIP开通页信息回调接口
	 *
	 */
	vipPage : function(callback){
		var me = this;

		if(callback){
			me.callCmd({cmd:136, callback: callback});
		}
	},
	/**
	 * 沃酷狗.免流量畅听”开通信息回调与状态更新接口
	 *
	 */
	woFreeInfo : function(callback){
		var me = this;

		if(callback){
			me.callCmd({cmd:137, callback: callback});
		}
	},
	/**
	 * “沃酷狗.免流量畅听”授权接口
	 */
	openWoFree : function(json, callback){
		var me = this,
		jsonStr = '';

		if(!json){
			return;
		}

		jsonStr = me.json2Str(json);

		if(callback){
			me.callCmd({cmd:138, jsonStr:jsonStr, callback: callback});
		}else{
			me.callCmd({cmd:138, jsonStr:jsonStr});
		}
	},
	/**
	 * 查询酷币信息接口
	 *
	 */
	getKgCoin : function(callback){
		var me = this;

		if(callback){
			me.callCmd({cmd:139, callback: callback});
		}
	},
	/**
	 * 客户端接口数据返回成功后通知客户端
	 *
	 */
	toClient : function(callback){
		var me = this;

		if(callback){
			me.callCmd({cmd:141, callback: callback});
		}else{
			me.callCmd({cmd:141});
		}
	},
	/**
	 * 打开客户端 支付宝/银联 支付
	 *
	 */
	toPay : function(json,callback){
		var me = this;

		var jsonStr = '';

		if(!json){
			return;
		}

		jsonStr = me.json2Str(json);

		if(callback){
			me.callCmd({cmd:142, jsonStr:jsonStr, callback: callback});
		}else{
			me.callCmd({cmd:142, jsonStr:jsonStr});
		}
	},
	/**
	 * 获取地理位置信息
	 *
	 */
	location : function(json,callback){
		var me = this,
			jsonStr = '';

		if(!json){
			return;
		}

		jsonStr = me.json2Str(json);

		if(callback){
			me.callCmd({cmd:143, jsonStr:jsonStr, callback: callback});
		}else{
			me.callCmd({cmd:143, jsonStr:jsonStr});
		}
	},

	/**
	 * 通知客户端更新用户VIP/音乐包信息
	 *
	 */
	getVipInfo : function(callback){
		var me = this;

		if(callback){
			me.callCmd({cmd:144, callback: callback});
		}else{
			me.callCmd({cmd:144});
		}
	},

	/**
	 * 跳转到挑歌历史页
	 */
	openHistory: function(callback) {
		var me = this;

		if(callback){
			me.callCmd({cmd:145, callback: callback});
		}else{
			me.callCmd({cmd:145});
		}
	},
	/**
	 * 添加歌曲到历史列表
	 */
	addSongToHistory: function(json, callback) {
		var me = this;

		if (!json) {
			return false;
		}

		var jsonStr = me.json2Str(json);

		if(callback){
			me.callCmd({cmd:146, jsonStr:jsonStr, callback: callback});
		}else{
			me.callCmd({cmd:146, jsonStr:jsonStr});
		}
	},
	/**
	 * 添加统计信息
	 */
	statistics: function(androidId, iosId, name, category, source, callback) {
		var me = this;
		var json;

		if (!androidId || !iosId) {
			return false;
		}
		json = {
			name: name,
			category: category,
			source: source || ""
		};

		if (me.chkAndroidApp()) {
			json.id = androidId;
		} else if(me.isIOS) {
			json.id = iosId;
		} else {
			return false;
		}

		var jsonStr = me.json2Str(json);

		if(callback){
			me.callCmd({cmd:147, jsonStr:jsonStr, callback: callback});
		}else{
			me.callCmd({cmd:147, jsonStr:jsonStr});
		}
	},
	/**
	 * 添加歌曲到历史列表
	 */
	isFirstEnter: function(json, callback) {
		var me = this;

		if (!json) {
			return false;
		}

		var jsonStr = me.json2Str(json);

		if(callback){
			me.callCmd({cmd:148, jsonStr:jsonStr, callback: callback});
		}else{
			me.callCmd({cmd:148, jsonStr:jsonStr});
		}
	},
	/**
	 * 显示或者隐藏内嵌页更多功能中的某些功能
	 */
	showHideFun: function(json, callback) {
		var me = this,
		jsonStr = '';

		if (!json) {
			return false;
		}

		jsonStr = me.json2Str(json);

		if(callback){
			me.callCmd({cmd:149, jsonStr:jsonStr, callback: callback});
		}else{
			me.callCmd({cmd:149, jsonStr:jsonStr});
		}
	},













	/*------- 乐库下 分类tab 使用命令号 (页面跳转用)------------*/
	/**
	 * 乐库下分类内嵌页跳转歌单页(单个歌单)
	 */
	categoryJumpSinglePlaylistPage: function(json, callback) {
		var me = this,
		jsonStr = '';

		if (!json) {
			return false;
		}

		jsonStr = me.json2Str(json);

		if(callback){
			me.callCmd({cmd:701, jsonStr:jsonStr, callback: callback});
		}else{
			me.callCmd({cmd:701, jsonStr:jsonStr});
		}
	},
	/**
	 * 乐库下分类内嵌页跳转专辑页(单个专辑)
	 */
	categoryJumpSingleAlbumlistPage: function(json, callback) {
		var me = this,
		jsonStr = '';

		if (!json) {
			return false;
		}

		jsonStr = me.json2Str(json);

		if(callback){
			me.callCmd({cmd:702, jsonStr:jsonStr, callback: callback});
		}else{
			me.callCmd({cmd:702, jsonStr:jsonStr});
		}
	},
	/**
	 * 乐库下分类内嵌页跳转歌手页(单个歌手)
	 */
	categoryJumpSingerPage: function(json, callback) {
		var me = this,
		jsonStr = '';

		if (!json) {
			return false;
		}

		jsonStr = me.json2Str(json);

		if(callback){
			me.callCmd({cmd:703, jsonStr:jsonStr, callback: callback});
		}else{
			me.callCmd({cmd:703, jsonStr:jsonStr});
		}
	},
	/**
	 * 乐库下分类内嵌页跳转对应分类标签页
	 */
	categoryJumpCategoryPage: function(json, callback) {
		var me = this,
		jsonStr = '';

		if (!json) {
			return false;
		}

		jsonStr = me.json2Str(json);

		if(callback){
			me.callCmd({cmd:704, jsonStr:jsonStr, callback: callback});
		}else{
			me.callCmd({cmd:704, jsonStr:jsonStr});
		}
	},
	/**
	 * 乐库下分类内嵌页跳到对应MV歌单页
	 */
	categoryJumpMVlistPage: function(json, callback) {
		var me = this,
		jsonStr = '';

		if (!json) {
			return false;
		}

		jsonStr = me.json2Str(json);

		if(callback){
			me.callCmd({cmd:705, jsonStr:jsonStr, callback: callback});
		}else{
			me.callCmd({cmd:705, jsonStr:jsonStr});
		}
	},
	/**
	 * 乐库下分类内嵌页跳转排行榜页
	 */
	categoryJumpRanklistPage: function(json, callback) {
		var me = this,
		jsonStr = '';

		if (!json) {
			return false;
		}

		jsonStr = me.json2Str(json);

		if(callback){
			me.callCmd({cmd:706, jsonStr:jsonStr, callback: callback});
		}else{
			me.callCmd({cmd:706, jsonStr:jsonStr});
		}
	},
	/*------- 乐库下 分类tab 使用命令号 (页面不跳转)------------*/
	/**
	 * 乐库下分类内嵌页跳转播放MV（跳转单个MV页面）
	 */
	categoryPlayMV: function(json, callback) {
		var me = this,
		jsonStr = '';

		if (!json) {
			return false;
		}

		jsonStr = me.json2Str(json);

		if(callback){
			me.callCmd({cmd:707, jsonStr:jsonStr, callback: callback});
		}else{
			me.callCmd({cmd:707, jsonStr:jsonStr});
		}
	},
	/**
	 * 乐库下分类内嵌页跳转播放电台
	 */
	categoryPlayFM: function(json, callback) {
		var me = this,
		jsonStr = '';

		if (!json) {
			return false;
		}

		jsonStr = me.json2Str(json);

		if(callback){
			me.callCmd({cmd:708, jsonStr:jsonStr, callback: callback});
		}else{
			me.callCmd({cmd:708, jsonStr:jsonStr});
		}
	},
	/*------- 乐库下 分类tab 使用命令号 (辅助功能)------------*/
	/**
	 * 分享按钮显示隐藏
	 */
	showHideRightTopShareBar: function(json, callback) {
		var me = this,
		jsonStr = '';

		if (!json) {
			return false;
		}

		jsonStr = me.json2Str(json);

		if(callback){
			me.callCmd({cmd:709, jsonStr:jsonStr, callback: callback});
		}else{
			me.callCmd({cmd:709, jsonStr:jsonStr});
		}
	},
	/**
	 * 内嵌页网页功能接口
	 */
	savePreReturnPage: function(json, callback) {
		var me = this,
		jsonStr = '';

		if (!json) {
			return false;
		}

		jsonStr = me.json2Str(json);

		if(callback){
			me.callCmd({cmd:710, jsonStr:jsonStr, callback: callback});
		}else{
			me.callCmd({cmd:710, jsonStr:jsonStr});
		}
	},
	/**
	 * 内嵌页网页功能接口
	 */
	currentHasBannerForSwipe: function(json, callback) {
		var me = this,
		jsonStr = '';

		if (!json) {
			return false;
		}

		jsonStr = me.json2Str(json);

		if(callback){
			me.callCmd({cmd:711, jsonStr:jsonStr, callback: callback});
		}else{
			me.callCmd({cmd:711, jsonStr:jsonStr});
		}
	},
	/**
	 * 原生创建桌面快捷方式（安卓）
	 */
	createDeskTopIcon: function(json, callback) {
		var me = this,
		jsonStr = '';

		if (!json) {
			return false;
		}

		jsonStr = me.json2Str(json);

		if(callback){
			me.callCmd({cmd:712, jsonStr:jsonStr, callback: callback});
		}else{
			me.callCmd({cmd:712, jsonStr:jsonStr});
		}
	},
	/**
	 * 查询当前页面是否位于 分类 子 tab栏
	 */
	getCategoryTabInfo : function(callback) {
		this.callCmd({cmd:713, callback:callback});
	},

	/**
	 * 关闭webview
	 */
	closeFrame: function(json) {
		var me = this,
		jsonStr = '';

		if(!json){
			return;
		}

		jsonStr = me.json2Str(json);
		me.callCmd({cmd:158, jsonStr:jsonStr});
	},

};

/**
 * 客户端调用网页端接口
 */
export const KgWebMobileCall = {
	/*客户端用户状态改变时回调*/
	userStatus : function(cmdid, jsonStr){},
	/*用户离开切换内嵌页回调*/
	pageStatus : function(cmdid, jsonStr){},
	/*歌曲分享成功后，回调通知网页*/
	shareStatus : function(cmdid, jsonStr){},
	/*播放状态回调*/
	playStatus : function(jsonStr){},
	/*上传回调*/
	uploadStatus : function(jsonStr){},
	/*充值回调*/
	rechargeStatus : function(jsonStr){},
	/*支付宝/银联 支付状态回调*/
	payStatus : function(jsonStr){},

	pageStatusNew: function (cmdid, jsonStr) {

	 },
};

/**
 * 外部页面调用 app
 */
export const KgWebToApp = {
	/*0：未知，1：安装，2：没安装*/
	isInstalled : 0,
	/*获取userAgent*/
	ua : navigator.userAgent || '',
	/**
	* 获取hidden属性
	*/
	getHidden : function(){
		var hidden,
		dc = document;

		if (typeof document.hidden !== "undefined") {
			hidden = "hidden";
		} else if (typeof document.mozHidden !== "undefined") {
			hidden = "mozHidden";
		} else if (typeof document.msHidden !== "undefined") {
			hidden = "msHidden";
		} else if (typeof document.webkitHidden !== "undefined") {
			hidden = "webkitHidden";
		}
		KgWebToApp.getHidden = function(){
			return hidden;
		};
		return KgWebToApp.getHidden();
	},
	/**
	* @method str2Json
	*
	* @param { String } jsonStr
	* @return { Object } json
	*/
	str2Json : function(jsonStr){
		var json = {};
		if(typeof jsonStr == "string"){
			try {
				json = JSON.parse(jsonStr);
			} catch (ex) {
				json = null;
			}
		}
		return json;
	},
	json2Str : function(json){
		var jsonStr = "";
		if(typeof json !== "string"){
			try {
				jsonStr = JSON.stringify(json);
			} catch (ex) {
				jsonStr = "";
			}
		}
		return jsonStr;
	},
	/**
     * 执行不返回数据的调用
     *
     * @method callCmd
     * @function
     * @param { Object }
	 * {
	 *  protocol:{ 协议头
	 *	ios : 'kugouurl://start.weixin/',
	 *	android:'kugou://start.weixin/'
	 *  },
	 *	cmd : Number(命令号),
	 *	jsonStr : {},
	 *  callback:Function(回调函数)
	 * }
     */
	callCmd: function(o) {
		var me = this,
		dc = document,
		ua = navigator.userAgent,
		isAndroid = /android|Adr/gi.test(ua),
		isIOS = /iphone|ipad|ipod/gi.test(ua),
		/*基本协协*/
		android_protocal = 'kugou://start.weixin',
		ios_protocal = 'kugouurl://start.weixin',
		/*最终跳转地址*/
		schemel_url = null,
		hidden = me.getHidden(),
		startTime,
		endTime,
		timeout = 1500,
		offtime = 2000,
		res;

		if(isAndroid){
			offtime = 1550;
		}

		if(isAndroid){
			//设置协议头
			schemel_url = android_protocal;
			if(o && o.protocal && o.protocal.android){
				schemel_url = o.protocal.android;
			}
		}else if(isIOS){
			//设置协议头
			schemel_url = ios_protocal;
			if(o && o.protocal && o.protocal.ios){
				schemel_url = o.protocal.ios;
			}
		}

		if(o && o.cmd){
			if(o.jsonStr){
				schemel_url += '?' + '{"cmd": '+ o.cmd +', "jsonStr": '+ o.jsonStr +'}';
			}else{
				schemel_url += '?' + '{"cmd": '+ o.cmd +'}';
			}
		}

		//debug
		if(dc.getElementById('mod_debugwin_set')){
			dc.getElementById('mod_debugwin_set').innerHTML = schemel_url;
		}

		//时间
		var start = (new Date()).valueOf();

		//其他系统直接回调
		if(!schemel_url){
			if(o && o.callback){
				o.callback();
			}
			return;
		}

		//debug
		/*if(o && dc.getElementById('mod_debugwin_set')){
			dc.getElementById('mod_debugwin_set').innerHTML = JSON.stringify(o);
		}
		if(dc.getElementById('mod_debugwin')){
			dc.getElementById('mod_debugwin').innerHTML = schemel_url;
		}*/

		//执行打开
		location.href = schemel_url;

		//是否超时
		setTimeout(function(){
			if(!document[hidden]){
				var end = (new Date()).valueOf();
				if((end - start) < offtime){
					if(o && o.callback){
						o.callback();
					}
				}
			}
		}, timeout);

	},
	/**
	 * 跳转到单曲下载
	 * @Param { Object }  参数
	 * @Param { Function } 失败回调
	 */
	downSongs : function(json, callback){
		var me = this,
		jsonStr = "";

		if(json){
			/*兼容IOS*/
			json['320size'] = json['320filesize'] || 0;
			json.m4asize = json.m4afilesize || 0;
			json.timelength = json.duration;
			json['320hash'] = json['320hash'] || 0;
			jsonStr = me.json2Str(json);
		}else{
			return;
		}

		if(callback){
			me.callCmd({cmd: 301, jsonStr: jsonStr, callback: callback});
		}else{
			me.callCmd({cmd: 301, jsonStr: jsonStr});
		}
	},
	/**
	 * 跳转到歌单详细页
	 * @Param { Object }  参数
	 * @Param { Function } 失败回调
	 */
	jumpPlaylist : function(json, callback){
		var me = this,
		jsonStr = "";
		jsonStr = me.json2Str(json);

		if(callback){
			me.callCmd({cmd: 302, jsonStr: jsonStr, callback: callback});
		}else{
			me.callCmd({cmd: 302, jsonStr: jsonStr});
		}
	},
	/**
	 * 跳转到自定义页面，如内嵌页
	 * @Param { Object }  参数
	 * @Param { Function } 失败回调
	 */
	jumpCustomPage : function(json, callback){
		var me = this,
		jsonStr = "";
		jsonStr = me.json2Str(json);

		if(callback){
			me.callCmd({cmd: 303, jsonStr: jsonStr, callback: callback});
		}else{
			me.callCmd({cmd: 303, jsonStr: jsonStr});
		}
	},
	/**
	 * 跳转到客户端相关页
	 * @Param { Object }  参数
	 * @Param { Function } 失败回调
	 */
	jumpClientPage : function(json, callback){
		var me = this,
		jsonStr = "";
		jsonStr = me.json2Str(json);

		if(callback){
			me.callCmd({cmd: 304, jsonStr: jsonStr, callback: callback});
		}else{
			me.callCmd({cmd: 304, jsonStr: jsonStr});
		}
	},
	/**
	 * 跳转到歌手详细页
	 * @Param { Object }  参数
	 * @Param { Function } 失败回调
	 */
	jumpSingerPage : function(json, callback){
		var me = this,
		jsonStr = "";
		jsonStr = me.json2Str(json);

		if(callback){
			me.callCmd({cmd: 305, jsonStr: jsonStr, callback: callback});
		}else{
			me.callCmd({cmd: 305, jsonStr: jsonStr});
		}
	},
	/**
	 * 跳转电台播放页（有声和名人电台），自动播放最新一期节目
	 * @Param { Object }  参数
	 * @Param { Function } 失败回调
	 */
	jumpRadio : function(json, callback){
		var me = this,
		jsonStr = "";
		jsonStr = me.json2Str(json);

		if(callback){
			me.callCmd({cmd: 306, jsonStr: jsonStr, callback: callback});
		}else{
			me.callCmd({cmd: 306, jsonStr: jsonStr});
		}
	},
	/**
	 * 跳转FM播放页，自动播放在线节目
	 * @Param { Object }  参数
	 * @Param { Function } 失败回调
	 */
	jumpFM : function(json, callback){
		var me = this,
		jsonStr = "";
		jsonStr = me.json2Str(json);

		if(callback){
			me.callCmd({cmd: 307, jsonStr: jsonStr, callback: callback});
		}else{
			me.callCmd({cmd: 307, jsonStr: jsonStr});
		}
	},
	/**
	 * 跳转 MV 详情页，并自动播放该MV
	 * @Param { Object }  参数
	 * @Param { Function } 失败回调
	 */
	jumpMV : function(json, callback){
		var me = this,
		jsonStr = "";
		jsonStr = me.json2Str(json);

		if(callback){
			me.callCmd({cmd: 308, jsonStr: jsonStr, callback: callback});
		}else{
			me.callCmd({cmd: 308, jsonStr: jsonStr});
		}
	},
	/**
	 * 打开客户端
	 */
	openApp : function(callback){
		var me = this;

		if(callback){
			me.callCmd({callback : callback});
		}else{
			me.callCmd();
		}
	}
};

const mobilecall = {
    KgMobileCall : KgMobileCall,
    KgWebMobileCall : KgWebMobileCall,
    KgWebToApp : KgWebToApp
};

window.KgMobileCall = mobilecall;
window.KgWebMobileCall = KgWebMobileCall;

export default mobilecall;
